#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")

// compile: gcc main.c -lwsock32 && a
// closesocket(clnt_sock)不能写在while循环里，会有一半的请求失败
int main()
{
  WORD ws_version = MAKEWORD(2, 2);
  WSADATA wsaData;
  WSAStartup(ws_version, &wsaData);
  SOCKET serv_sock;
  serv_sock = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in serv_addr;
  memset(&serv_addr, 0, sizeof(serv_addr));
  SOCKADDR  clnt_addr;
  int clt_sz = sizeof(SOCKADDR);
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(8002);
  printf("running at 127.0.0.1:8002\n");
  bind(serv_sock, (SOCKADDR *)&serv_addr, sizeof(SOCKADDR));
  listen(serv_sock, 20);
  SOCKET clnt_sock;
  char head[] = "HTTP/1.1 200 OK\r\ncontent-type: text/plain; charset=utf-8\r\nContent-length:11\r\n\r\n我owo world";
  int head_sz = sizeof(head);
  while (1)
  {
    clnt_sock = accept(serv_sock, (SOCKADDR *)&clnt_addr, &clt_sz);
    send(clnt_sock, head, head_sz, 0);
  }
  closesocket(clnt_sock);
  closesocket(serv_sock);
  WSACleanup();
  return 0;
}
